﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Foothill.Utils;
using GGFW.Entity.DocumentsManage;
using GGFW.Utils.Constants;
using GGFW.DataAccess.DocumentsManage;
using GGFW.DataAccess.Constants;

namespace GGFW.BusinessLogic.DocumentsManage
{
    public class OrdersControl
    {
        /// <summary>
        /// 订单数据层访问类
        /// </summary>
        private readonly OrdersDao dal = new OrdersDao();

        #region 保存订单主表信息
        /// <summary>
        /// 保存订单主表信息
        /// </summary>
        /// <param name="productFileds"></param>
        /// <returns></returns>
        public int SaveOrder(string orderFileds)
        {
            int order_id;
            SqlTransaction trans = null;
            SqlConnection conn = null;

            try
            {
                conn = new SqlConnection(SQLConfig.FoothillDataBaseConnectionString);
                conn.Open();
                trans = conn.BeginTransaction();
                //成功             
                order_id = dal.SaveOrder(orderFileds); //新增任务
            }
            catch (Exception ex)
            {
                order_id = 0;
                if (trans != null)
                {
                    trans.Rollback();
                }
            }
            finally
            {
                trans = null;
                if (conn != null && !ConnectionState.Closed.Equals(conn.State))
                {
                    conn.Close();
                }
            }

            return order_id;
        }
        #endregion

        #region 保存订单Grid表信息
        /// <summary>
        /// 保存订单Grid表信息
        /// </summary>
        /// <param name="ordergridFileds"></param>
        /// <param name="order_id"></param>
        /// <returns></returns>
        public bool SaveOrderGrid(string ordergridFileds, int order_id)
        {
            SqlTransaction trans = null;
            SqlConnection conn = null;

            try
            {
                conn = new SqlConnection(SQLConfig.FoothillDataBaseConnectionString);
                conn.Open();
                trans = conn.BeginTransaction();
                //成功             
                dal.SaveOrderGrid(ordergridFileds, order_id);
            }
            catch (Exception ex)
            {

                if (trans != null)
                {
                    trans.Rollback();
                }
                return false;
            }
            finally
            {
                trans = null;
                if (conn != null && !ConnectionState.Closed.Equals(conn.State))
                {
                    conn.Close();
                }
            }

            return true;
        }
        #endregion

        #region 分页查询订单信息(v_orders)
        /// <summary>
        /// 分页查询订单信息(v_orders)
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="v_ordersEntity"></param>
        /// <returns></returns>
        public FoothillPageData<v_orders> SearchViewOrders(int PageSize, int PageIndex, v_orders v_ordersEntity)
        {
            FoothillPageData<v_orders> result = new FoothillPageData<v_orders>();

            try
            {   //成功
                int totalCount = 0;
                List<v_orders> models = this.Search_NakederViewOrders(PageSize, PageIndex, v_ordersEntity, out totalCount);

                result.Records = (models ?? new List<v_orders>());
                result.totalCount = totalCount;
                result.Success = true;
            }
            catch (Exception ex)
            {
                //失败
                GGFW.Utils.Constants.CommonUtil.SealExceptionToFoothillResult(result, ex);
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="v_ordersEntity"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List<v_orders> Search_NakederViewOrders(int PageSize, int PageIndex, v_orders v_ordersEntity, out int totalCount)
        {
            if (v_ordersEntity == null)
            {
                totalCount = 0;
                return null;
            }

            return dal.QueryByPageViewOrders(PageSize, PageIndex, v_ordersEntity, out totalCount);
        }
        #endregion

        #region 分页查询订单详细信息(v_ordersinfo)
        /// <summary>
        ///  获取订单详细信息(v_ordersinfo)
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="v_ordersinfoEntity"></param>
        /// <returns></returns>
        public FoothillPageData<v_ordersinfo> GetViewOrdersInfo(int PageSize, int PageIndex, v_ordersinfo v_ordersinfoEntity)
        {
            FoothillPageData<v_ordersinfo> result = new FoothillPageData<v_ordersinfo>();

            try
            {   //成功
                int totalCount = 0;
                List<v_ordersinfo> models = this.Search_NakederViewOrdersInfo(PageSize, PageIndex, v_ordersinfoEntity, out totalCount);

                result.Records = (models ?? new List<v_ordersinfo>());
                result.totalCount = totalCount;
                result.Success = true;
            }
            catch (Exception ex)
            {
                //失败
                GGFW.Utils.Constants.CommonUtil.SealExceptionToFoothillResult(result, ex);
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="view_ordersEntity"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List<v_ordersinfo> Search_NakederViewOrdersInfo(int PageSize, int PageIndex, v_ordersinfo v_ordersinfoEntity, out int totalCount)
        {
            if (v_ordersinfoEntity == null)
            {
                totalCount = 0;
                return null;
            }

            return dal.QueryByPageViewOrdersInfo(PageSize, PageIndex, v_ordersinfoEntity, out totalCount);
        }
        #endregion

        #region 获取View_MO
        /// <summary>
        /// 获取View_MO
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="v_moEntity"></param>
        /// <returns></returns>
        public FoothillPageData<v_mo> GetViewMO(int PageSize, int PageIndex, v_mo v_moEntity)
        {
            FoothillPageData<v_mo> result = new FoothillPageData<v_mo>();

            try
            {   //成功
                int totalCount = 0;
                List<v_mo> models = this.Search_NakederViewMO(PageSize, PageIndex, v_moEntity, out totalCount);

                result.Records = (models ?? new List<v_mo>());
                result.totalCount = totalCount;
                result.Success = true;
            }
            catch (Exception ex)
            {
                //失败
                GGFW.Utils.Constants.CommonUtil.SealExceptionToFoothillResult(result, ex);
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="v_moEntity"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List<v_mo> Search_NakederViewMO(int PageSize, int PageIndex, v_mo v_moEntity, out int totalCount)
        {
            if (v_moEntity == null)
            {
                totalCount = 0;
                return null;
            }

            return dal.QueryByPageViewMO(PageSize, PageIndex, v_moEntity, out totalCount);
        }
        #endregion

        #region 更新订单表(orders)信息
        /// <summary>
        /// 更新订单表(orders)信息
        /// </summary>
        /// <param name="TabName">表名</param>
        /// <param name="FiledName">字段名</param>
        /// <param name="FiledValue">字段值</param>
        /// <returns></returns>
        public FoothillOperationResult<bool> UpdateOrdersInfo(string OrderID, string TabName, string FiledName, string FiledValue)
        {
            FoothillOperationResult<bool> result = new FoothillOperationResult<bool>();

            try
            {
                //成功
                result.Success = dal.UpdateOrdersInfo(OrderID, TabName, FiledName, FiledValue); ;
            }
            catch (Exception ex)
            {
                //失败
                GGFW.Utils.Constants.CommonUtil.SealExceptionToFoothillResult(result, ex);
            }

            return result;
        }

        #endregion

        #region 更新订单表(ordersgrid)信息
        /// <summary>
        /// 更新订单表(ordersgrid)信息
        /// </summary>
        /// <param name="OgID"></param>
        /// <param name="OrderID"></param>
        /// <param name="FieldName"></param>
        /// <param name="FieldValue"></param>
        /// <returns></returns>
        public FoothillOperationResult<ordersgrid> UpdateOrdersGridfield(string OgID, string OrderID, string FieldName, string FieldValue)
        {
            FoothillOperationResult<ordersgrid> result = new FoothillOperationResult<ordersgrid>();

            try
            {
                //成功
                result.Data = new List<ordersgrid>();
                result.Data.Add(dal.UpdateOrdersGridfield(OgID, OrderID, FieldName, FieldValue));
                result.Success = true;
            }
            catch (Exception ex)
            {
                //失败
                GGFW.Utils.Constants.CommonUtil.SealExceptionToFoothillResult(result, ex);
            }

            return result;
        }
        #endregion

        #region 删除订单
        /// <summary>
        /// 删除订单
        /// </summary>
        /// <param name="order_id"></param>
        /// <returns></returns>
        public FoothillOperationResult<orders> DelOrdersByID(int order_id)
        {
            FoothillOperationResult<orders> result = new FoothillOperationResult<orders>();

            try
            {
                dal.DelOrdersByID(order_id);
                result.success = true;
            }
            catch
            {
                result.success = false;
            }

            return result;
        }
        #endregion
    }
}
